﻿using AdverseEventReportSystem.BLL.Escort;
using AdverseEventReportSystem.BLL.EscortManageSys;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Escort.Controllers
{
    public class EscortPersonnelController : BaseController
    {
        // GET: Escort/EscortPersonnel
        public ActionResult Index()
        {
            ViewBag.CurrDepCode = loginUser.CurrDepCode;
            ViewBag.JobTitle = loginUser.JobTitle;
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", ConfigHelper.GetConfigInt("pagesize"));
            string date = WebUtils.GetString("date");
            string stopDate = WebUtils.GetString("stopDate");
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            int comeFrom = WebUtils.GetInt("comeFrom");
            if (comeFrom == -1) comeFrom = 9;
            int escortType = WebUtils.GetInt("escortType", 2);
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfString serviceResult = service.GetEscortList(date, stopDate, departmentCode, (byte)comeFrom, (byte)escortType, pageIndex, pageSize);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                var result = new { Total = jsonobj.total, Rows = jsonobj.data };
                return JsonDate(result);
            }

            return JsonDate(new { Total = 0, Rows = new DataTable() });
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 获取陪护人员的当前汇总信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSummaryInfo()
        {
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            OperateResult resultInfo = EscortPersonnelManager.GetSummaryInfo(departmentCode);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取陪护人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetEscortInfoByID(string id)
        {
            OperateResult resultInfo = EscortPersonnelManager.GetPersonnelInfo(id);
            return JsonDate(resultInfo);
        }


        /// <summary>
        /// 添加/编辑陪护人员
        /// </summary>
        /// <param name="id"></param>
        /// <param name="jsonInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(string id, string jsonInfo)
        {
            OperateResult resultInfo = EscortPersonnelManager.Save(id, loginUser.Personnel_id, jsonInfo);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            OperateResult resultInfo = EscortPersonnelManager.Delte(id, loginUser.Personnel_id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Stop(string id)
        {
            OperateResult resultInfo = EscortPersonnelManager.Stop(id, loginUser.Personnel_id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 陪护终止人员【陪护人员查询】
        /// </summary>
        /// <returns></returns>
        public ActionResult Expire()
        {
            ViewBag.CurrDepCode = loginUser.CurrDepCode;
            ViewBag.JobTitle = loginUser.JobTitle;
            return View();
        }

        /// <summary>
        /// 查询陪护终止人员列表【查询陪护人员列表】
        /// </summary>
        /// <returns></returns>
        public ActionResult SearchExpire()
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", ConfigHelper.GetConfigInt("pagesize"));
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            string expireStartDate = WebUtils.GetString("expireStartDate");
            string expireEndDate = WebUtils.GetString("expireEndDate");
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            int comeFrom = WebUtils.GetInt("comeFrom");
            if (comeFrom == -1) comeFrom = 9;
            int escortType = WebUtils.GetInt("escortType", 2);
            int keyAreaFlag = WebUtils.GetInt("keyAreaFlag", -1);
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfString serviceResult = service.GetEscortExpireList(startDate, endDate, expireStartDate, expireEndDate, departmentCode, (byte)comeFrom, (byte)escortType, keyAreaFlag, pageIndex, pageSize);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                var result = new { Total = jsonobj.total, Rows = jsonobj.data };
                return JsonDate(result);
            }

            return JsonDate(new { Total = 0, Rows = new DataTable() });
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintView()
        {
            return View();
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <returns></returns>
        public ActionResult Print()
        {
            return View();
        }

        /// <summary>
        /// 获取陪护证打印数据
        /// </summary>
        /// <returns></returns>
        [LoginAttribute(IsNeed = false)]
        public ActionResult GetEscortCardPrint()
        {
            string eid = WebUtils.GetString("eid");
            OperateResult resultInfo = EscortPersonnelManager.GetPersonnelInfo(eid);
            if (resultInfo.Result == 1)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                var result = new { Rows = jsonobj };
                return JsonDate(result);
            }
            else
            {
                return JsonDate(null);
            }
        }

        /// <summary>
        /// 获取出院证明数据
        /// </summary>
        /// <returns></returns>
        [LoginAttribute(IsNeed = false)]
        public ActionResult GetOutCardPrint()
        {
            string eid = WebUtils.GetString("eid");
            string pid = WebUtils.GetString("pid");
            string vid = WebUtils.GetString("vid");
            if (eid != "")
            {
                OperateResult resultInfo = EscortPersonnelManager.GetPersonnelInfo(eid);
                if (resultInfo.Result == 1)
                {
                    var jsonobj = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                    var result = new { Rows = jsonobj };
                    return JsonDate(result);
                }
            }
            else
            {
                EscortManageSysSoapClient service = new EscortManageSysSoapClient();
                ResultHelperOfString serviceResult = service.GetPatientInfoByID(pid, vid);
                if (serviceResult.S)
                {
                    var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                    jsonobj[0].ESCORT_ID = jsonobj[0].PATIENT_ID;
                    jsonobj[0].ESCORT_NAME = jsonobj[0].PATIENT_NAME;
                    jsonobj[0].KINSHIP_NAME = "本人";
                    var result = new { Rows = jsonobj };
                    return JsonDate(result);
                }
            }

            return JsonDate(null);
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public ActionResult Export()
        {
            string date = WebUtils.GetString("date");
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            int comeFrom = WebUtils.GetInt("comeFrom");
            if (comeFrom == -1) comeFrom = 9;
            string stopDate = "";
            int escortType = WebUtils.GetInt("escortType", 2);
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfArrayOfByte serviceResult = service.GetEscortListForExport(date, stopDate, departmentCode, (byte)comeFrom, (byte)escortType);
            if (serviceResult.S)
            {
                string fileName = ("陪护人员-") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                return File(serviceResult.D, "application/vnd.ms-excel", fileName);
            }
            return View();
        }

        /// <summary>
        /// 导出陪护结束人员Excel
        /// </summary>
        /// <returns></returns>
        public ActionResult ExpireExport()
        {
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            string expireStartDate = WebUtils.GetString("expireStartDate");
            string expireEndDate = WebUtils.GetString("expireEndDate");
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            int comeFrom = WebUtils.GetInt("comeFrom");
            if (comeFrom == -1) comeFrom = 9;
            int escortType = WebUtils.GetInt("escortType", 2);
            int keyAreaFlag = WebUtils.GetInt("keyAreaFlag", -1);
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfArrayOfByte serviceResult = service.GetEscortExpireForExport(startDate, endDate, expireStartDate, expireEndDate, departmentCode, (byte)comeFrom, (byte)escortType, keyAreaFlag);
            if (serviceResult.S)
            {
                string fileName = ("陪护结束人员-") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                return File(serviceResult.D, "application/vnd.ms-excel", fileName);
            }
            return View();
        }
    }
}